#!/usr/bin/env python    
# -*- coding: utf-8 -*-    
#导入smtplib和MIMEText    
import smtplib    
from email.mime.text import MIMEText    
    
#配置信息    
mail_to             ='renzhewudihu@163.com'    
mail_server         ='smtp.163.com'  #qq:'smtp.qq.com    
mail_user_name      ='hu_xiao_liang'    
mail_user_passwd    ='xxx.'    
mail_postfix        ='163.com'    
    
info_list = [ mail_server, mail_user_name,mail_user_passwd ,mail_postfix]    
    
''' 
to_list:    目的邮件地址  
sub:        邮件的主题  
content:    邮件的内容  
'''    
def send_mail(list,to_list,sub,content):    
    #设置服务器，用户名、口令以及邮箱的后缀    
    
#    assert type(to_list) == list    
    mail_server=str(list[0])
    mail_user_name=str(list[1])
    mail_user_passwd=str(list[2])
    mail_postfix=str(list[3])
        
    print type(mail_server),mail_server    
         
    scr_addr='send machine'+'<'+mail_user_name+'@'+mail_postfix+'>'    
    msg = MIMEText(content,_subtype='text/plain',_charset='gb2312')    
    msg['Subject'] = sub    
    msg['From'] = scr_addr
    msg['To'] = to_list    
    try:    
        s = smtplib.SMTP()    
        s.connect(mail_server)    
        s.login(mail_user_name,mail_user_passwd)    
        s.sendmail(scr_addr, to_list, msg.as_string())    
        s.close()    
        print '1'    
        return True    
    except Exception, e:    
        print 'mail error:'    
        print str(e)    
        return False    
if __name__ == '__main__':    
    if send_mail(info_list,mail_to,'titile0','hi david'):
        print 'send success'    
    else:    
        print 'send failed'    
